package com.paomi.client.view;

import android.app.Activity;
import android.os.Handler;
import android.support.annotation.StringRes;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.paomi.client.MyApplication;
import com.paomi.client.R;


/**
 * Created by Administrator on 2017/6/15 0015.
 * 全局Loading动画
 */

public class LoadingDialog implements View.OnClickListener {

    private ViewGroup decorView;
    private FrameLayout rootView;
    private TextView tipsView;
    private Runnable hideRunnable = new Runnable() {
        @Override
        public void run() { // 延时300ms执行，避免多个请求时刚消失又弹出
            if (decorView != null && rootView != null) decorView.removeView(rootView);
        }
    };

    private LoadingDialog() {}

    private static final LoadingDialog mInstance = new LoadingDialog();

    public static LoadingDialog get() {
        return mInstance;
    }

    private Handler mHandler = new Handler();

    public void showLoading() {
        showLoading(0, "");
    }

    public void showLoading(String tips) {
        showLoading(0, tips);
    }

    public void showLoading(long duration) {
        showLoading(duration, "");
    }

    public void showLoading(long duration, @StringRes int tips) {
        Activity curActivity = MyApplication.getAppContext().getCurActivity();
        if(curActivity == null)return;
        showLoading(duration, curActivity.getString(tips));
    }

    public void showLoading(long duration, String tips) {
        if(duration < 0) {
            hideLoading();
            return;
        }
        mHandler.removeCallbacks(hideRunnable);
        Activity curActivity = MyApplication.getAppContext().getCurActivity();
        if(curActivity == null)return;
        decorView = (ViewGroup) curActivity.getWindow().getDecorView().findViewById(android.R.id.content);
        if(decorView == null) return;
        if(rootView == null) {
            rootView = new FrameLayout(curActivity);
            rootView.setLayoutParams(getRootLayoutParams());
            rootView.setId(R.id.dialog_root);

//            View loading = LayoutInflater.from(curActivity).inflate(R.layout.layout_loading, rootView, false);
            View loading = View.inflate(curActivity, R.layout.layout_loading, null);
            tipsView = (TextView) loading.findViewById(R.id.tv_loading_tips);

            rootView.addView(loading);
            rootView.setClickable(true);
            rootView.setOnClickListener(this);
        }

//        if(TextUtils.isEmpty(tips)) {
//            tipsView.setVisibility(View.GONE);
//        } else {
//            tipsView.setVisibility(View.VISIBLE);
//            tipsView.setText(tips);
//        }
        if(rootView.getParent() != null) ((ViewGroup)rootView.getParent()).removeView(rootView);
        decorView.addView(rootView);

        if(duration > 0) {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    hideLoading();
                }
            }, duration);
        }

    }

    /** 延时300ms隐藏，当一个界面有多个请求避免出现 显示 - 隐藏 - 显示*/
    public void hideLoading() {
        mHandler.removeCallbacksAndMessages(null);
        mHandler.postDelayed(hideRunnable, 200);
    }

    private ViewGroup.LayoutParams getRootLayoutParams() {
        return new FrameLayout.LayoutParams(-1, -1);
    }

    @Override
    public void onClick(View v) {
    }
}
